package com.alesj.newsfeed.web.form;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alesj.newsfeed.data.Contact;
import com.alesj.newsfeed.data.Group;
import com.alesj.newsfeed.web.object.SelectSend;
import com.generalynx.common.constants.WebConst;
import com.generalynx.common.utils.CollectionUtils;
import com.generalynx.ecos.data.dao.IBasicDAO;
import com.generalynx.ecos.data.dao.IBasicDAOAware;
import com.generalynx.ecos.data.types.Status;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class ExportForm extends SimpleFormController implements IBasicDAOAware {

    protected IBasicDAO basicDAO;

    public ExportForm() {
        setCommandClass(SelectSend.class);
    }

    protected Map referenceData(HttpServletRequest request) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        Collection groups = getBasicDAO().findEntity(Group.class);
        model.put("groups", groups);
        return model;
    }

    protected ModelAndView onSubmit(HttpServletRequest request,
                                    HttpServletResponse response,
                                    Object command,
                                    BindException errors) throws Exception {
        SelectSend selectSend = (SelectSend) command;
        if (selectSend.isPrepare()) {
            HttpSession session = request.getSession(false);
            boolean isConjunction = "AND".equalsIgnoreCase(selectSend.getType());
            Set excluded = new TreeSet();
            int[] exludedIds = selectSend.getExclude();
            CollectionUtils.asSet(exludedIds, excluded);
            int[] entriesIds = selectSend.getEntry();
            Status[] acceptedStatus = new Status[]{Status.DEFAULT, Status.DISABLED};
            Collection contacts = getBasicDAO().findContacts(entriesIds, exludedIds, isConjunction, acceptedStatus);
            if (contacts == null || contacts.isEmpty()) {
                return new ModelAndView("msgView", WebConst.MSG_CODE, "empty_contacts");
            } else {
                session.setAttribute("contacts", contacts);
                return new ModelAndView(getSuccessView());
            }
        } else {
            List<Contact> elements = new ArrayList<Contact>();
            for (int contactId : selectSend.getContact()) {
                elements.add(getBasicDAO().loadContact(contactId));
            }
            return new ModelAndView(selectSend.getType() + "View", "elements", elements);
        }
    }

    /* setters && getters */

    public IBasicDAO getBasicDAO() {
        return basicDAO;
    }

    public void setBasicDAO(IBasicDAO basicDAO) {
        this.basicDAO = basicDAO;
    }

}
